﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace EC21_EDiamond
{
    public partial class ThemSanPham : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lbErrorDM.Visible = false;
            lbErrorTH.Visible = false;
            lbThongBaoKQThem.Visible = false;
            lbErrorImg.Visible = false;
        }

        //them 1 san pham moi vao csdl
        protected void btnDongY_Click(object sender, EventArgs e)
        {
            SanPhamDTO spDTO = new SanPhamDTO();
            spDTO.TenSP = txtTenSP.Text;
            spDTO.Mota = txtMoTa.Text;
            spDTO.Loaida = txtLoaiDa.Text;
            spDTO.Trongluong = float.Parse(txtTrongLuong.Text);
            spDTO.Giagoc = int.Parse(txtGiaGoc.Text);
            if (txtGiaKM.Text == "")
            {
                spDTO.GiaKM = int.Parse("");
            }
            else 
            {
                spDTO.GiaKM = int.Parse(txtGiaKM.Text);
            }            
            spDTO.Chatlieu = txtChatLieu.Text;
            spDTO.SlTon = int.Parse(txtSoLuongTon.Text);
            spDTO.MaDM = int.Parse(cbxDanhMuc.SelectedValue.ToString());
            spDTO.MaTH = int.Parse(cbxThuongHieu.SelectedValue.ToString());
            if (cbTopSP.Checked == true)
            {
                spDTO.IsIndex = 1;
            }
            else
            {
                spDTO.IsIndex = 0;
            }
            if (upAnh.HasFile)
            {
                string filename = Path.GetFileName(upAnh.FileName);
                if (CheckFileType(filename))
                {
                    upAnh.SaveAs(Server.MapPath("./Content/fontend/img/") + filename);
                    spDTO.HinhAnh = "./Content/fontend/img/" + filename;
                }
                else
                {
                    lbErrorImg.Visible = true;
                    lbErrorImg.Text = "Vui lòng chọn đúng định dạng ảnh";
                    return;
                }

            }

            SanPhamBUS spBUS = new SanPhamBUS();
            if (spBUS.ThemSanPham(spDTO) == 1)
            {
                lbThongBaoKQThem.ForeColor = System.Drawing.Color.Green;
                lbThongBaoKQThem.Visible = true;
                lbThongBaoKQThem.Text = "Thêm thành công";
            }
            else
            {
                lbThongBaoKQThem.ForeColor = System.Drawing.Color.Red;
                lbThongBaoKQThem.Visible = true;
                lbThongBaoKQThem.Text = "Thêm thất bại";
            }
        }

        //Kiem tra dinh dang co dung la anh ko
        private bool CheckFileType(string FileName)
        {
            string ext = Path.GetExtension(FileName);
            if (ext.Equals(".png")|| ext.Equals(".jpg") || ext.Equals(".gif"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}